package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.Submission;
import com.example.stuwork.service.SubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作业提交控制器
 */
@RestController
@RequestMapping("/submissions")
public class SubmissionController {
    
    @Autowired
    private SubmissionService submissionService;
    
    /**
     * 获取所有作业提交记录
     */
    @GetMapping("")
    public Result<List<Submission>> getAllSubmissions() {
        return submissionService.getAllSubmissions();
    }
    
    /**
     * 根据ID获取提交记录详情
     */
    @GetMapping("/{id}")
    public Result<Submission> getSubmissionById(@PathVariable("id") Long submissionId) {
        return submissionService.getSubmissionById(submissionId);
    }
    
    /**
     * 根据作业ID获取提交记录列表
     */
    @GetMapping("/assignment/{assignmentId}")
    public Result<List<Submission>> getSubmissionsByAssignmentId(@PathVariable("assignmentId") Long assignmentId) {
        return submissionService.getSubmissionsByAssignmentId(assignmentId);
    }
    
    /**
     * 根据学生ID获取提交记录列表
     */
    @GetMapping("/student/{studentId}")
    public Result<List<Submission>> getSubmissionsByStudentId(@PathVariable("studentId") String studentId) {
        return submissionService.getSubmissionsByStudentId(studentId);
    }
    
    /**
     * 根据作业ID和学生ID获取提交记录
     */
    @GetMapping("/assignment/{assignmentId}/student/{studentId}")
    public Result<Submission> getSubmissionByAssignmentIdAndStudentId(
            @PathVariable("assignmentId") Long assignmentId,
            @PathVariable("studentId") String studentId) {
        return submissionService.getSubmissionByAssignmentIdAndStudentId(assignmentId, studentId);
    }
    
    /**
     * 根据作业ID和状态获取提交记录列表
     */
    @GetMapping("/assignment/{assignmentId}/status/{status}")
    public Result<List<Submission>> getSubmissionsByAssignmentIdAndStatus(
            @PathVariable("assignmentId") Long assignmentId,
            @PathVariable("status") Integer status) {
        return submissionService.getSubmissionsByAssignmentIdAndStatus(assignmentId, status);
    }
    
    /**
     * 根据教师ID获取需要批改的提交记录列表
     */
    @GetMapping("/teacher/{teacherId}/grading")
    public Result<List<Submission>> getSubmissionsForGradingByTeacherId(@PathVariable("teacherId") String teacherId) {
        return submissionService.getSubmissionsForGradingByTeacherId(teacherId);
    }
    
    /**
     * 添加提交记录
     */
    @PostMapping("")
    public Result<Submission> addSubmission(
            @RequestPart("submission") Submission submission,
            @RequestPart(value = "file", required = false) MultipartFile file) {
        return submissionService.addSubmission(submission, file);
    }
    
    /**
     * 更新提交记录
     */
    @PutMapping("/{id}")
    public Result<Submission> updateSubmission(
            @PathVariable("id") Long submissionId,
            @RequestBody Submission submission) {
        submission.setSubmissionId(submissionId);
        return submissionService.updateSubmission(submission);
    }
    
    /**
     * 批改作业
     */
    @PutMapping("/{id}/grade")
    public Result<Submission> gradeSubmission(
            @PathVariable("id") Long submissionId,
            @RequestParam("score") Integer score,
            @RequestParam("feedback") String feedback,
            @RequestParam("teacherId") String teacherId) {
        return submissionService.gradeSubmission(submissionId, score, feedback, teacherId);
    }
    
    /**
     * 批量下载提交文件
     */
    @GetMapping("/assignment/{assignmentId}/download")
    public Result<String> downloadSubmissionFiles(
            @PathVariable("assignmentId") Long assignmentId,
            HttpServletResponse response) {
        return submissionService.downloadSubmissionFiles(assignmentId, response);
    }
    
    /**
     * 导出作业提交记录
     */
    @GetMapping("/assignment/{assignmentId}/export")
    public Result<String> exportSubmissions(
            @PathVariable("assignmentId") Long assignmentId,
            HttpServletResponse response) {
        return submissionService.exportSubmissions(assignmentId, response);
    }
    
    /**
     * 删除提交记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteSubmission(@PathVariable("id") Long submissionId) {
        return submissionService.deleteSubmission(submissionId);
    }
    
    /**
     * 获取提交状态统计
     */
    @GetMapping("/assignment/{assignmentId}/stats")
    public Result<Map<String, Object>> getSubmissionStats(@PathVariable("assignmentId") Long assignmentId) {
        return submissionService.getSubmissionStats(assignmentId);
    }
} 